package edu.biwu.controller.upload;

import edu.biwu.utils.ResponseData;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Log4j2
public class UploadController {

    /*使用springmvc 实现文件上传 无论单文件还是多文件都遵循一下步骤
     * 1.获取上传文件的名称 判断是否为空 不为空则生成新的文件名UUID
     * 2.创建上传文件的目录 判断是否存在 不存在则创建 new File(目录的路径)
     * 3.将上传的文件写入到指定目录中 File serverFile = new File(目录的路径,文件名) 进行拼接 例如 E:/upload/uuid.jpg
     * 4.返回上传成功信息*/


    // 上传文件的目录
    final String UPLOAD_DIR_PATH = "E:\\Projects\\JavaWeb\\biwujiaoyu-javaee-project\\day12-springmvc\\springmvc02\\src\\main\\webapp\\uploadDir";

    @PostMapping("/single")
    public ResponseData uploadSingle(@RequestParam("uploadFile") MultipartFile uploadFile) {

        //1.判断上传的文件是否为空
        if (uploadFile == null) {
            return new ResponseData(400, "文件不能上传为空");
        }
        //2.获取上传文件的名称并且生成新的文件名保证唯一
        String filename = uploadFile.getOriginalFilename();
        String extensive = filename.substring(filename.lastIndexOf("."));
        String newFilename = UUID.randomUUID() + extensive;

        //3.创建上传文件的目录
        File uploadDir = new File(UPLOAD_DIR_PATH);//创建上传文件的目录
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        //4.将上传的文件写入到指定目录中
        File serverFile = new File(uploadDir, newFilename);
        try {
            uploadFile.transferTo(serverFile);
            return new ResponseData(200, "文件上传成功");
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return new ResponseData(500, "文件上传失败");
        } finally {
        }
    }

    @PostMapping("/multiple")
    public ResponseData uploadMutiple(@RequestParam("uploadFiles") MultipartFile[] uploadFiles) {
        if (uploadFiles.length == 0 && uploadFiles[0].isEmpty()) {
            return new ResponseData(400, "上传文件不能为空");
        }
        File uploadDir = new File(UPLOAD_DIR_PATH);

        ArrayList<String> fileNames = new ArrayList<>();
        try {
            for (MultipartFile file : uploadFiles) {
                if (file != null) {
                    String fileName = file.getOriginalFilename();
                    String extension = fileName.substring(fileName.lastIndexOf("."));
                    String newFileName = UUID.randomUUID() + extension;
                    File serverFile = new File(uploadDir, newFileName);//把文件夹和文件名拼接起来
                    file.transferTo(serverFile);
                    fileNames.add(fileName);
                }
            }
            return new ResponseData(200, "文件上传成功" + fileNames);
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return new ResponseData(500, "文件上传失败");
        }
    }
}
